package org.itboys.mobile.service.mongo.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayStoreDto;
import org.itboys.mobile.entity.mongo.SundayStore;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by zhangyang on 2017/12/27.
 * 圣代_实体店_service
 */
@Service
public class SundayStoreService extends BaseMongoService<SundayStore>{

    @Autowired
    private SundayUploadService uploadService;

    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }

    @Override
    protected Class<SundayStore> getEntityClass() {
        return SundayStore.class;
    }

    @Autowired
    private ResourceHolder resourceHolder;


    /**
     * 获取dto
     */
    public List<SundayStoreDto> select(HttpServletRequest request){
        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request, "");
            Map<String,Object> param = new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        PageResult<SundayStore> pageResult = super.containpageQuery(request,containsparam, param);

        List<SundayStore> physicalStoreList = pageResult.getData();

        //组装dto
        List<SundayStoreDto> list = packageMuilt(physicalStoreList);
        return list;
    }

    /**
     * 组装dto(多)
     */
    public List<SundayStoreDto> packageMuilt(List<SundayStore> physicalStoreList){
        List<SundayStoreDto> list = new ArrayList<SundayStoreDto>();
        for(SundayStore store : physicalStoreList){
            SundayStoreDto dto = packageSingle(store);
            list.add(dto);
        }
        return list;
    }

    /**
     * 组装dto(单)
     */
    public SundayStoreDto packageSingle(SundayStore store){
        String imgRoot = resourceHolder.getStringValue("imgRoot");
        SundayStoreDto dto = new SundayStoreDto();
        dto.setId(store.getId());
        dto.setName(store.getName());
        dto.setImage(imgRoot+store.getImage());
        dto.setMobile(store.getMobile());
        dto.setDesc(store.getDesc());
        //是直辖市则不显示省名称
        if(store.getProvinceName().equals(store.getCityName())){
            dto.setAddress(store.getCityName()+store.getDistrictName()+store.getAddress());
        }else{
            dto.setAddress(store.getProvinceName()+store.getCityName()+store.getDistrictName()+store.getAddress());
        }
        return dto;
    }



    /**
     * 保存实体店
     * @param store 实体
     * @param imageFile 文件file
     * @throws Exception
     */
    public void save(SundayStore store,MultipartFile imageFile) throws Exception{
        Map<String, String> map = uploadService.uploadSingle(imageFile);
        if(StringUtils.isNotEmpty(map.get(uploadService.UPLOAD_HALF_PATH))){
            store.setImage(map.get(uploadService.UPLOAD_HALF_PATH));
        }
        if(store.getId() == 0L){
           super.save(store);
        }else{
            super.updateExceptEmpty(store.getId(),store);
        }
    }

    /**
     * 获取实体店详情
     * @param storeId
     */
    public SundayStoreDto getDetail(Long storeId){
          return packageSingle(super.getById(storeId));
    }
}
